﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GTA
{
    public partial class FrmLogin : BaseForm
    {
        #region Starting Form
        public FrmLogin()
        {
            InitializeComponent();
        }
        private void FrmLogin_Load(object sender, EventArgs e)
        {

        }
        #endregion

        #region Events
        private void cmdConfirm_Click(object sender, EventArgs e)
        {
            Login();
        }
        private void cmdCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }
        #endregion

        #region Events
        private void txtLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.CompareTo(' ') == 0)
                e.Handled = true;
        }
        #endregion

        #region Methods
        private void Login()
        {
            try
            {
                if (ValidateForm())
                {
                    BusinessLogic.GTA bl = new BusinessLogic.GTA();
                    Program.LoggedUser = bl.AuthenticateUser(txtLogin.Text.Trim(), Cryptography.Util.DESCryptographer.Encrypt(txtPassword.Text.Trim()));
                    if (Program.LoggedUser == null)
                    {
                        MessageBox.Show("Usuário ou senha inválido!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        txtPassword.Text = String.Empty;
                        txtLogin.Focus();
                        txtLogin.SelectAll();
                    }
                    else
                    {
                        Program.LoggedUser.UserProfile.FunctionalityList = new BusinessLogic.GTA().ListProfilerFunctionalities(Program.LoggedUser.UserProfile);
                        this.Tag = true;
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Logar no sistema", ex);
            }
        }
        private bool ValidateForm()
        {
            bool valid = true;
            StringBuilder message = new StringBuilder("Informe os campos obrigatórios:\r\n");
            if (String.IsNullOrEmpty(txtLogin.Text.Trim()))
            {
                valid = false;
                message.Append("Login");
            }
            if (String.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                valid = false;
                message.Append("Senha");
            }
            if (!valid)
                MessageBox.Show(message.ToString(), "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return valid;
        }
        private void Cancel()
        {
            this.Tag = false;
            this.Close();
        }
        #endregion
    }
}
